/*
 * =============================================================================
 * Copyright© by Benjamin Sempere,
 * All rights reserved.
 * =============================================================================
 * Author  : Benjamin Sempere
 * E-mail  : benjamin@sempere.org
 * Homepage: www.sempere.org
 * =============================================================================
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.sempere.gwt.toolbox.core.client.common;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;

public abstract class AbstractSubject implements Locatee, Observable {

	protected boolean subjectChanged;
	protected Map<String, Observer> observersMap = new HashMap<String, Observer>();

	/**
	 * Default class constructor
	 */
	public AbstractSubject() {
	}

	public final void addObserver(Observer observer) {
		if (observer == null) {
			throw new IllegalArgumentException("Cannot add observer when it is null.");
		}
		if (this.observersMap.containsKey(observer.getObserverName())) {
			throw new IllegalArgumentException("Observer with name [" + observer.getObserverName() + "] is already registered to the observable with name [" + this.getObservableName() + "].");
		}

		this.observersMap.put(observer.getObserverName(), observer);
		GWT.log("Observer with name [" + observer.getObserverName() + "] was successfully registered to the observable with name [" + this.getObservableName() + "].", null);
	}

	public final void addObservers(Collection<Observer> observers) {
		if (observers == null) {
			throw new IllegalArgumentException("Cannot add a collection of observers when it is null.");
		}

		for (Observer observer : observers) {
			this.addObserver(observer);
		}
	}

	public final void removeObserver(Observer observer) {
		if (observer == null) {
			throw new IllegalArgumentException("Cannot remove observer when its is null.");
		}
		if (!this.observersMap.containsKey(observer.getObserverName())) {
			throw new IllegalArgumentException("No observer with name [" + observer.getObserverName() + "] is registered to the observable with name [" + this.getObservableName() + "].");
		}

		this.observersMap.remove(observer.getObserverName());
		GWT.log("Observer with name [" + observer.getObserverName() + "] was successfully unregistered to the observable with name [" + this.getObservableName() + "].", null);
	}

	public final void removeObservers() {
		this.observersMap.clear();
		GWT.log("Observers list observable with name [" + this.getObservableName() + "] was successfully cleared.", null);
	}

	public int countObservers() {
		return this.observersMap.size();
	}

	public void notifyObservers() {
		if (this.hasChanged()) {
			for (Observer observer : this.observersMap.values()) {
				observer.update(this);
			}

			this.clearChanged();
		}
	}

	public final void notifyObservers(Object context) {
		if (this.hasChanged()) {
			for (Observer observer : this.observersMap.values()) {
				observer.update(this, context);
			}

			this.clearChanged();
		}
	}

	public boolean hasChanged() {
		return this.subjectChanged;
	}

	public void setChanged() {
		this.subjectChanged = true;
		GWT.log("The observable with name [" + this.getObservableName() + "] has changed.", null);

		this.notifyObservers(this);
	}

	public void clearChanged() {
		this.subjectChanged = false;
		GWT.log("The observable with name [" + this.getObservableName() + "] is up to date.", null);
	}
}
